package com.example.redisdemo1.Service;

import com.example.redisdemo1.Model.Student;
import com.example.redisdemo1.Model.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Cacheable(cacheNames = {"mystudent"})//将运行结果缓存
    public List<Student> select(){
        return studentMapper.select();
    }
    @CacheEvict(cacheNames = {"mystudent"})//将name为mystudent的缓存删除
    public void delete(){
        //Todo
    }
    public String sum(){
        if(redisTemplate.opsForValue().get("sum") == null){
            String  scoresum = String.valueOf(studentMapper.sum());
            redisTemplate.opsForValue().set("sum",scoresum,15, TimeUnit.SECONDS);
        }
        return redisTemplate.opsForValue().get("sum");
    }
}
